package cn.com.ddapp.ddm.ui.order;

import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.ActivityManager;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.order.AsDetailEntity;
import cn.com.ddapp.ddm.bean.order.ReturnDetail;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TLog;

/**
 * Created by caopenghui on 2016-08-28.
 */
public class AfterSalesDetailActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;

    @BindView(R.id.stateText)
    TextView stateText;
    @BindView(R.id.fundAmount)
    TextView fundAmount;
    @BindView(R.id.fundTime)
    TextView fundTime;
    @BindView(R.id.rtType)
    TextView rtType;
    @BindView(R.id.rtAmount)
    TextView rtAmount;
    @BindView(R.id.rtReason)
    TextView rtReason;
    @BindView(R.id.rtRemark)
    TextView rtRemark;
    @BindView(R.id.rtNo)
    TextView rtNo;
    @BindView(R.id.rtTime)
    TextView rtTime;
    @BindView(R.id.cancelRt)
    TextView cancelRt;

    @BindView(R.id.stateImg)
    ImageView stateImg;

    private Integer orderId;
    private Integer goodsId;

    @Override
    public void initView() {
        title.setText(BaseApplication.getIntstance().getResources().getString(R.string.sales_detail));
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
    }

    @Override
    public void initData() {
        orderId = getIntent().getIntExtra("orderId",0);
        goodsId = getIntent().getIntExtra("goodsId",0);
        requestData();
    }

    private void requestData() {

        mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);

        final OkUtilCallback okUtilCallback =  new OkUtilCallback(mEmptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                TLog.log(response);
                AsDetailEntity entity = gson.fromJson(response,AsDetailEntity.class);
                if((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))&&entity.getLogin()){
                    mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                }else if((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))){
                    BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                }else {
                    ReturnDetail returnDetail = entity.getReturnDetail();
                    fundAmount.setText(""+returnDetail.getReturnAmount());
                    fundTime.setText(returnDetail.getSuccessTime());
                    rtType.setText(returnDetail.getReturnType());
                    rtAmount.setText(""+returnDetail.getReturnAmount());
                    rtReason.setText(returnDetail.getContent());
                    rtRemark.setText(returnDetail.getReson());
                    rtNo.setText(returnDetail.getExpressNo());
                    rtTime.setText(returnDetail.getCreateTime());
                }

            }
        };
        OKUtilAPI.post(OKUtilAPI.afterDetail, "userId=" + SPUtils.getStringData(getApplicationContext(), SPUtils.USERID)+"&goodsId="+goodsId+"&orderId="+orderId, okUtilCallback);
    }

    @Override
    public int getContentViewId() {
        return R.layout.after_sales_detail_activity;
    }

    @Override
    protected String getFragmentTag() {
        return AfterSalesDetailActivity.class.getSimpleName();
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==back.getId()){
            ActivityManager.getInstance().finishActivity();
        }
    }
}
